package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.DiscountApplyBean;
import com.project.mapper.IDiscountApplyMapper;
import com.project.service.IDiscountApplyService;
import com.project.util.SnowIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 促销申请服务类
 */
@Service
@Transactional
public class DiscountApplyServiceImpl implements IDiscountApplyService {
    @Autowired
    private IDiscountApplyMapper discountApplyMapper;
    @Override
    public String addDiscountApply(DiscountApplyBean discountApplyObj) {
        if (discountApplyObj !=null){//判断对象不为空
            discountApplyObj.setDiscountApplyId(SnowIdUtils.uniqueLongHex());
            discountApplyMapper.insert(discountApplyObj);
            return "ok";
        }
       return "no";
    }

    @Override
    public String updateStatusByDiscountApplyId(String discountApplyId, String status) {
        if (status ==null || status =="") {//判断状态是否为空
            return "审核状态为空";
        }
        QueryWrapper<DiscountApplyBean> qw=new QueryWrapper<>();
        qw.eq("discountApplyId",discountApplyId);
        //通过申请编号查询
        DiscountApplyBean discountApplyBean = discountApplyMapper.selectOne(qw);
        if (discountApplyBean ==null){//判断查出的对象是否是空对象
            return "申请编号有误";
        }
        discountApplyBean.setStatus(status);
        discountApplyMapper.updateById(discountApplyBean);
        return "ok";


    }

    @Override
    public String updateReasonByDiscountApplyId(String discountApplyId, String reason) {
        if (reason ==null || reason =="") {//判断审核结果是否为空
            return "审核结果为空";
        }
        QueryWrapper<DiscountApplyBean> qw=new QueryWrapper<>();
        qw.eq("discountApplyId",discountApplyId);
        //通过申请编号查询
        DiscountApplyBean discountApplyBean = discountApplyMapper.selectOne(qw);
        if (discountApplyBean ==null){//判断查出的对象是否是空对象
            return "申请编号有误";
        }
        discountApplyBean.setReason(reason);
        discountApplyMapper.updateById(discountApplyBean);
        return "ok";
    }


    @Override
    public IPage<DiscountApplyBean> findDiscountApply(int pageNO) {
        return discountApplyMapper.selectPage(new Page<>(pageNO,3),null);
    }

}
